#pragma once
#include <Winsock2.h>

class WSocket
{
public:
	WSocket(void);
	~WSocket(void);

	bool Create();
	bool Connect(const char *addr, UINT port);
    void Close();

	int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
	int Receive(void* lpBuf, int nBufLen, int nFlags = 0);

	int Timeout(int recvtimeout, int sendtimeout);
    bool Block(bool isBlock);

	bool Listen(int maxCon = 5);
	bool Accept(WSocket &conSock, sockaddr_in *paddr);
    bool Bind(sockaddr_in *paddr);

	int Error();

	static DWORD WorkThread(LPVOID lParam);
    bool isRun;
    WSAEVENT skEvent;

protected:
	virtual void OnClose(int errno);

private:
	SOCKET fd;
	
	HANDLE hThread;
};
